<?php

/**
 * 前台栏目模型
 *
 * @package         YDCMS
 * @author          QQ:644157559 <644157559@qq.com>
 * @version         $Id: ContypeModel.class.php v1.0 2014-06-13 17:27:03 $
 */
namespace Home\Model;
class ContypeModel extends CommonModel{
    private $idArray;
    private $idCounter;
    protected $_link = array(
        'Model' => array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' => 'Model',
            'foreign_key' => 'model_id',
            'as_fields' => 'name:model_name,addtable',
        )
    );
    public function _initialize() {
        parent::_initialize();
        $this->idArray = '';
        $this->idCounter = 0;
    }

    /**
     * 根据条件获取栏目
     * @param type $where
     * @param type $field
     * @param type $limit
     * @param type $orderby
     * @return type
     */
    public function getType($where = '', $field = '*', $limit = "0,100", $orderby = 'sortrank ASC', $soncat = false) {
        $result = $this->cache(TRUE)->field($field)->where($where)->order($orderby)->limit($limit)->select();
        if(!isArrayAndNotEmpty($result)){
            return NULL;
        }
        
        if($field == "*"){
            foreach ($result as $key => $val){
                $result[$key]['typeurl'] = GetOneTypeUrlA($val);
                //$result[$key]['typeurl'] = GetTypeUrl($val['id'], MfTypedir($val['typedir']), $val['isdefault'], $val['defaultname'], $val['ispart'], $val['namerule2']);
            }
        }
        if($soncat === true){
            foreach ($result as $key => $val){
                $where['reid'] = $val['id'];
                $result[$key]["soncat"] = $this->getType($where, $field, $limit, $orderby, $soncat);
            }
        }
        return $result;
    }
    
    /**
     * 获取栏目详细
     * @param int $typeid
     * @param bool $relation
     * @return type
     */
    public function getTypeInfos($typeid, $relation = true) {
        $result = $this->relation($relation)->getById(intval($typeid));
        $result['typeurl'] = GetOneTypeUrlA($result);
        return $result;
    }
    /**
     * 获取子ID
     * @param type $reid
     * @param type $model_id
     * @return type
     */
    public function getSonId($reid, $model_id = 0) {
        $this->idArray = '';
        $res = $this->getSonIds($reid, $model_id);
        //var_dump($res);
        return $res;
    }
    /**
     * 获取子ID
     * @param type $reid
     * @param type $model_id
     * @return type
     */
    private function getSonIds($reid, $model_id = 0) {
        $this->idArray[$this->idCounter] = intval($reid);
        $this->idCounter++;
        $where['reid'] = $reid;
        if($model_id != 0){
            $where['model_id'] = $model_id;
        }
        $type = $this->where($where)->field("id")->select();
        if(isArrayAndNotEmpty($type)){
            foreach ($type as $value) {
                $this->getSonIds($value['id'], $model_id);
            }
        }
        return $this->idArray;
    }
}
